<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" v-show="showSearch" label-width="0px">
      <el-row :gutter="20" class="queryFormRow">
      <el-col :span="6">
        <el-form-item label="" prop="lsh">
        <el-input
          v-model="queryParams.lsh"
          placeholder="申请编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      </el-col>
      <el-col :span="6">
          <el-form-item label="" prop="qymc">
            <el-input
              v-model="queryParams.qymc"
              placeholder="企业名称"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="" prop="xmmcs">
          <el-select v-model="queryParams.xmmcs" filterable multiple collapse-tags size="small" placeholder="应用项目" style="width:100%" clearable>
              <el-option
                v-for="dict in dict.type.sys_project_ls"
                :key="dict.value"
                :label="dict.label"
                :value="dict.label"
              />
            </el-select>
      </el-form-item>
      </el-col>
      <el-col :span="6">
          <el-form-item label="" prop="flag">
            <el-select v-model="queryParams.flag" placeholder="当前状态" style="width:100%" clearable>
              <el-option
                v-for="dict in hisStatusArr"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
      </el-col>
      <el-col :span="6">
          <el-form-item label="" prop="ks">
            <el-select v-model="queryParams.ks" size="small" placeholder="所属科室" style="width:100%" clearable>
              <el-option
                v-for="item in deptOptions"
                :key="item.deptId"
                :label="item.deptName"
                :value="item.deptName">
              </el-option>
            </el-select>
          </el-form-item>
      </el-col>
      <!-- <el-col :span="6">
          <el-form-item label="" prop="dxpid">
            <el-input
              v-model="queryParams.dxpid"
              placeholder="DXPID"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
      </el-col> -->
      <el-col :span="6">
        <el-form-item label="">
          <el-date-picker
            v-model="dateRange"
            style="width: 100%"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="daterange"
            range-separator="-"
            start-placeholder="申请开始日期"
            end-placeholder="申请结束日期"
            :default-time="['00:00:00', '23:59:59']"
          ></el-date-picker>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="">
          <el-date-picker
            v-model="dateRanges"
            style="width: 100%"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="daterange"
            range-separator="-"
            start-placeholder="受理开始日期"
            end-placeholder="受理结束日期"
            :default-time="['00:00:00', '23:59:59']"
          ></el-date-picker>
        </el-form-item>
      </el-col>
      <!-- <el-col :span="6">
        <el-form-item label="">
          <el-date-picker
            v-model="createTime"
            style="width: 100%"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="daterange"
            range-separator="-"
            start-placeholder="创建开始日期"
            end-placeholder="创建结束日期"
            :default-time="['00:00:00', '23:59:59']"
          ></el-date-picker>
        </el-form-item>
      </el-col> -->
    </el-row>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
        :disabled="multiple"
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table ref="multipleTable" v-loading="loading" :data="finishedList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" fixed type="index" width="55" align="center" />
      <el-table-column label="申请编号" fixed align="center" prop="lsh" width="140">
        <template slot-scope="scope">
          <el-link type="primary" @click="handleFlowRecord(scope.row)">{{scope.row.lsh}}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="企业名称" fixed prop="qymc" align="center"  :show-overflow-tooltip="true">
      </el-table-column>
      <el-table-column label="应用项目" align="center" prop="xmmc" :show-overflow-tooltip="true"/>
      <el-table-column label="所属科室/系统" align="center" prop="ks" width="120" />
      <!-- <el-table-column label="DXPID" align="center" prop="dxpid" width="100"/> -->
      <el-table-column label="当前状态" align="center" prop="flag" width="200">
        <template slot-scope="scope">
          <span>{{ flagCN(scope.row.flag) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="申请日期" sortable align="center" prop="applytime" width="150"/>
      <el-table-column label="受理日期" sortable align="center" prop="slrq" width="150"/>
      <!-- <el-table-column label="创建日期" sortable align="center" prop="createTime" width="150"/> -->
      <!-- <el-table-column label="耗时" align="center" prop="duration" width="180"/> -->
      <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width" width="100">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleFlowRecord(scope.row)"
          >查看详情</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-tickets"
            @click="handleRevoke(scope.row)"
            v-hasPermi="['workflow:process:revoke']"
          >撤回
          </el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { historyList,getexpList } from '@/api/businessModule/historicalData.js';
import { hisStatus }  from '@/utils/historyStatus.js'
import { revokeProcess } from "@/api/workflow/finished";
import { deptListAll } from "@/api/administeration/askForLeave.js";

export default {
  name: "HistoricalData",
  dicts: ['sys_project_ls', 'approval_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 所属科室
      deptOptions: [],
      // 状态
      hisStatusArr:hisStatus,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 已办任务列表数据
      finishedList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      src: "",
      dateRange: [],
      dateRanges: [],
      createTime:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        xmmcs: [],
        ks: null,
        lsh: null,
        qymc: null,
        flag: null,
        dxpid:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      formData: {},
      definitionId: null,
      Code:"CODE_YW"
    };
  },
   // 监听路由回来的地址进行刷新数据
  // watch: {
  //   $route(newVal, oldVal) {
  //     if (newVal.path == '/businessModule/merge') {
  //       this.getList()
  //     }

  //   }
  // },
   // 利用activated函数来进行函数调用，这样可以保证每次进入界面都能刷新界面内容
  activated() {
    this.getList()
  },
  created() {
    this.getList()
  },
  methods: {
    // 状态回显
    flagCN(val) {
      if (this.hisStatusArr[val]) {
        return this.hisStatusArr[val].label
      } else {
        return val
      }
      
    },
    // 下拉部门
    async getDeptListAll() {
      const deptres = await deptListAll()
      this.deptOptions = deptres.data
    },
    /** 查询流程定义列表 */
    getList() {
      this.loading = true;
      let obj = {}
      if (this.dateRange != null && this.dateRange.length>0 ) {
        obj = {
          applyForStartDate:this.dateRange[0],
         applyForEndDate:this.dateRange[1]
        }
      }
      let objNew = {}
      if (this.dateRanges != null && this.dateRanges.length>0 ) {
        objNew = {
          previousNodeStartDate:this.dateRanges[0],
         previousNodeEndDate:this.dateRanges[1]
        }
      }
      // let createObj = {}
      // if (this.createTime != null && this.createTime.length>0 ) {
      //   createObj = {
      //     createStartDate:this.createTime[0],
      //    createEndDate:this.createTime[1]
      //   }
      // }
      historyList({ ...this.queryParams, xmmcs: this.queryParams.xmmcs.toString(), ...obj, ...objNew, }).then(response => {
        this.finishedList = response.rows;
        this.total = response.total;
        this.loading = false;
       });
      this.getDeptListAll()
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        category: null,
        key: null,
        tenantId: null,
        deployTime: null,
        derivedFrom: null,
        derivedFromRoot: null,
        parentDeploymentId: null,
        engineVersion: null
      };
      this.resetFormData("form");
    },
    setIcon(val){
      if (val){
        return "el-icon-check";
      }else {
        return "el-icon-time";
      }

    },
    setColor(val){
      if (val){
        return "#2bc418";
      }else {
        return "#b3bdbb";
      }

    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.dateRanges = [];
      this.createTime = []
      this.resetFormData("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.lsh)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增流程定义";
    },
    /** 流程流转记录 */
    handleFlowRecord(row) {
      const nameArr = [
        "快件KJ类系统激活码重置",
        "I-KEY卡变更申请",
        "QP系统激活码重置",
        "电子账册系统激活码重置"
      ]
      if (nameArr.includes(row.xmmc)) {
        this.$message.error('您暂无权限查看此项目详情!')
      } else {
         this.$router.push({
        path: '/workflow/process/historydetail',
        query: {
          lsh: row.lsh
        }
      })
      }
     
    },
    /** 撤回任务 */
    handleRevoke(row) {
      const params = {
        procInsId: row.procInsId,
        taskId: row.taskId
      };
      revokeProcess(params).then(res => {
        this.$modal.msgSuccess(res.msg);
        this.getList();
      });
    },
    /** 导出按钮操作 */
    async handleExport() {
      console.log(this.ids);
      const res = await getexpList({ lshs: this.ids })
      console.log(res,'导出数据');
      let list = [];
      // if(this.ids.length > 0){
      //   this.ids.forEach(ele => {
      //    let  obj = this.finishedList.find(item => item.lsh==ele);
      //     list.push(obj)

      //   });
      // //   list.forEach(item=>{
      // //     this.dict.type.ration_status.forEach(titem=>{
      // //     if(item.approvalStatus == titem.value){  
      // //       this.$set(item,'approvalStatus',titem.label)
      // //     }
      // //   })
      // //  })
      //   console.log(list,"88888888888888");
      //   exportExe(list).then(res=>{
          if(res){
          const blob = new Blob([res],{type: 'application/vnd.ms-excel'});
          const fileName ="历史应用项目数据.xlsx";
          if ("download" in document.createElement("a")) {
            // 非IE下载
            const elink = document.createElement("a");
            elink.download = fileName;
            elink.style.display = "none";
            elink.href = URL.createObjectURL(blob);
            document.body.appendChild(elink);
            elink.click();
            URL.revokeObjectURL(elink.href); // 释放URL 对象
            document.body.removeChild(elink);
          } else {
            // IE10+下载
            navigator.msSaveBlob(blob, fileName);
          }
            this.$refs.multipleTable.clearSelection();
          }

      //  })

      // }
    }
  }
};
</script>
<style lang="scss" scoped>
.queryFormRow{
    display:flex;
    flex-wrap: wrap;
}
</style>
